๐Ÿ”ฅFinal Project Day 3 - Grid

๐Ÿง™๐Ÿปโ€โ™‚๏ธFlex ๋งŒ ์“ฐ๋ฉด ๋˜์ง€ Grid ๋Š” ๋ญฃํ• ๋ผ๊ณ  ์จ?

/* 3 columns ๋ฅผ ๊ฐ€์ง€๊ณ  ์‹ถ์–ด์„œ flex-wrap: wrap ์†์„ฑ์„ ์คฌ๋Š”๋ฐ, */

.father {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
/* space-between ๋„ ์ฃผ์—ˆ๋‹ค. */

.child {
  background: goldenrod;
  flex-basis: 33%;
  color: white;
  font-size: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}

์ปฌ๋Ÿผ์ด ์„ธ๊ฐœ์ธ ๊ฒƒ์„ ํ‘œํ˜„ํ•˜๊ณ ์ž flex-basis ๋ฅผ 33% ๋ฅผ ์ฃผ๊ณ  ๋ถ€๋ชจ ์š”์†Œ์˜ flex-wrap ์„ wrap ์œผ๋กœ ์ฃผ์—ˆ๋”๋‹ˆ ์ ์ ˆ ํ•œ ๋ชจ์–‘์ด ๋‚˜์˜จ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ html ์—์„œ ์ž์‹ ๋ฐ•์Šค๋ฅผ ํ•˜๋‚˜ ๋” ๋„ฃ์–ด์„œ ์ด ๋„ค๊ฐœ๊ฐ€ ๋œ ๋ชจ์Šต์€ ์–ด๋–ค๊ฐ€?

๊ทธ๋ฆผ์„ ํ™•์ธํ•ด ๋ณด์ž.

wrong way flex

์Œ.. 1๋ฒˆ ๋ฐ•์Šค์™€ 4๋ฒˆ ๋ฐ•์Šค์˜ ๊ฐ„๊ฒฉ (๋งˆ์ง„) ์„ ๋„์šฐ๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ˆ˜๊ณ ๋ฅผ ํ•œ๋‹ค.

์ฒซ๋ฒˆ์งธ์™€ ๋„ค ๋ฒˆ์งธ ๋ฐ•์Šค์˜ ๊ฐ„๊ฒฉ์„ ๋„์šฐ๋ ค๊ณ  ๋งˆ์ง„์„ ๊ฐœ๋ณ„์ ์œผ๋กœ!!!! ์ฃผ๋Š” ์ˆ˜๊ณ ๋ฅผ ํ•œ๋‹ค ๋Š” ์˜๋ฏธ์ด๋‹ค.

.child:nth-child(4) {
  margin-top: 10px;
}

๋–จ์–ด์ง„๋‹ค ํ•˜์ง€๋งŒ ๋‹ค์„ฏ๋ฒˆ์งธ ๋ฐ•์Šค๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค๋ฉด ์–ด๋–จ๊นŒ?

๊ทธ ๋‹ค์„ฏ๋ฒˆ์งธ ๋ฐ•์Šค์—์„œ ๋‚ด๊ฐ€ ์˜๋„ํ•˜๋ ค๋Š” ๋ฐ”๋Š” 2๋ฒˆ ๋ฐ•์Šค ์•„๋ž˜์— ๋ฐ•์Šค 5๋ฒˆ์ด ์œ„์น˜ํ•˜๊ฒŒ ํ•˜๋Š”๊ฑด๋ฐ.. ๋ง์ด๋‹ค.

wrong way flex2

Flex ๋ฅผ ์“ฐ๊ณ  space-between ์„ ์“ฐ๊ณ .. ํ•˜ ์•ˆ๋œ๋‹ค. ์•ˆ๋ผ.

์ด๊ฒŒ ๋ฐ”๋กœ Flex ์˜ ํ•œ๊ณ„์ด๊ณ  ๊ทธ๋ž˜์„œ ๋ฐ”๋กœ ๊ทธ๋ฆฌ๋“œ(Grid) ๋ฅผ ๋ฐฐ์›Œ์•ผ ํ•˜๋Š” ์ด์œ ์ด๋‹ค.

๐Ÿง™๐Ÿปโ€โ™‚๏ธgrid-template- ์™€ -gap ์œผ๋กœ Grid ํ…œํ”Œ๋ฆฟ (์—‘์…€) ๋งŒ๋“ค๊ธฐ

๋ฐ•์Šค๋Š” ์„ธ ๊ฐœ ์ •๋„ ๋†“๊ณ , ์ฒ˜์Œ css ์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

Flexbox ์ฒ˜๋Ÿผ Grid ๋„ ๋ถ€๋ชจ ์š”์†Œ์—์„œ ์ปจํŠธ๋กคํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ์•„๋ž˜์ฒ˜๋Ÿผ ์ž‘์„ฑํ–ˆ๋‹ค.

.father {
  display: grid;
}

.child {
  background: goldenrod;
  color: white;
  font-size: 50px;

  display: flex;
  justify-content: center;
  align-items: center;
}

3 column grid ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ์–ด๋–ค ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์•ผ ํ• ๊นŒ?

1. grid-template-columns, column-gap, row-gap

html ์ƒ์—์„œ child ๋ฐ•์Šค๋ฅผ ์•„ํ™‰๊ฐœ ์ •๋„ ๋งŒ๋“ค์–ด ๋†“๊ณ  ์•„๋ž˜์˜ ์†์„ฑ์„ ์ ์šฉํ•ด ๋ณด์ž.

grid-template-columns: 250px 250px 250px;

basic grid 1

์˜คํ˜ธ.. ์ข‹๋‹ค์ข‹์•„.. ๊ทผ๋ฐ ์„ธ๋กœ ์ค„๋กœ ํ•˜๋‚˜, ๋‘˜, ์…‹ ์ด 3์ค„์˜ ์ปฌ๋Ÿผ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์„œ๋กœ ์ข€ ๊ฐ„๊ฒฉ์„ ๋„์›Œ์ฃผ๊ณ  ์‹ถ์ง€ ์•Š์„๊นŒ?

column-gap: 10px;

column gap

์˜ค์˜ค ๋„์›Œ์ง€๋Š”๊ตฌ๋งŒ, ๊ทธ๋Ÿฐ๋ฐ ์ฒซ์งธ ํ–‰ ๋ถ€ํ„ฐ ๊ฐ ํ–‰๋งˆ๋‹ค gap ์„ ์ฃผ๊ณ  ์‹ถ๋‹ค.

row-gap: 10px;

row gap

์˜คํ˜ธ, ๊ทธ๋Ÿฐ๋ฐ gap ์ด ์ปฌ๋Ÿผ, ๋กœ์šฐ ๋ชจ๋‘ ๊ฐ™๋‹ค๋ฉด ์•„๋ž˜์˜ css ์ฝ”๋“œ๋กœ ํ•œ์ค„ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

gap: 10px;

2. grid-template-rows

1๋ฒˆ์˜ column ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ ํ–‰ (row) ์— ๋†’์ด๋ฅผ ์ฃผ๊ธฐ ์œ„ํ•ด row grid ๋ฅผ ๋งŒ๋“ค์ž.

grid-template-rows: 100px 50px 300px;

basic grid 2

์˜คํ˜ธ๋ผ, ์ด๋Ÿฐ ๋Š๋‚Œ์ด๊ตฌ๋‚˜.

๐Ÿง™๐Ÿปโ€โ™‚๏ธ ์ตœ์ข… CSS ์ฝ”๋“œ

.father {
  display: grid;
  grid-template-columns: 250px 250px 250px;
  gap: 10px;
  grid-template-rows: 100px 50px 300px;
}

.child {
  background: goldenrod;
  color: white;
  font-size: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}

์ด์ œ ๊ถ๊ธˆํ•œ ๊ฒƒ์€ ์ € ํ•œ์นธ ํ•œ์นธ์˜ Cell ์„ ์–ด๋–ป๊ฒŒ ๋ณ‘ํ•ฉ ํ•ด์„œ ๋ ˆ์ด์•„์›ƒ์„ ๊ตฌ์„ฑํ•  ๊ฒƒ์ธ๊ฐ€ ์ด๋‹ค.


Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook